package com.grom.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import com.dispacher.context.ContextHolder;
import com.grom.po.Department;
import com.grom.po.GcmsUser;
import com.grom.po.RelationDepToUser;
import com.grom.service.DepartmentService;
import com.grom.service.GcmsUserService;
import com.grom.service.RelationDepToUserService;
import com.grom.util.JsonUtil;

public class GcmsUserAction {
	public void gotoList() {
		List<GcmsUser> allUsers = GcmsUserService.instance.getAllUser();
		ContextHolder.setRequestAttribute("allUsers", allUsers);
		ContextHolder.forward("/WEB-INF/jsp/user/update.jsp");
	}

	public void doSave() {
		GcmsUser user = (GcmsUser) ContextHolder
				.initFromTheRequest(GcmsUser.class);
		if (user.getID() == null || user.getID().trim().equals("")) {
			GcmsUserService.instance.add(user);
		} else {
			GcmsUserService.instance.update(user);
		}
		addRelationToDep(user);
		ContextHolder.sendRedirect(ContextHolder.findInstance().getContextUri()
				+ "/GcmsUserAction/gotoList.do");
	}

	private void addRelationToDep(GcmsUser user) {
		RelationDepToUser depToUser = (RelationDepToUser) ContextHolder
				.initFromTheRequest(RelationDepToUser.class);
		if (depToUser.getDepartmentID().equals("")) {
			return;
		}
		depToUser.setUserID(user.getID());
		RelationDepToUserService.instance.add(depToUser);
	}

	public void doDelete() {
		String id = ContextHolder.findInstance().getRequest()
				.getParameter("deleteId");
		System.out.println("| GcmsUserAction | doDelete | id | " + id);
		GcmsUser user = new GcmsUser();
		user.setID(id);
		GcmsUserService.instance.delete(user);
		ContextHolder.sendRedirect(ContextHolder.findInstance().getContextUri()
				+ "/GcmsUserAction/gotoList.do");
	}

	public void conditionSelect() {
		String userName_s = ContextHolder.findInstance().getRequest()
				.getParameter("userName_s");
		String remark_s = ContextHolder.findInstance().getRequest()
				.getParameter("remark_s");
		String sql = "select * from gcmsuser where username like '%"
				+ userName_s + "%' and remark like '%" + remark_s + "%'";
		List<GcmsUser> allUsers = GcmsUserService.instance
				.selectByCondition(sql);
		ContextHolder.setRequestAttribute("allUsers", allUsers);
		ContextHolder.forward("/WEB-INF/jsp/user/update.jsp");
	}

	public void getDetail() {
		String id = ContextHolder.findInstance().getRequest()
				.getParameter("itemid");
		GcmsUser user = GcmsUserService.instance.getUserByID(id);
		Department dep = DepartmentService.instance.getDepartmentByUserId(id);
		String json = JsonUtil.parseToJson(user);
		json = json.replace("}", ",'departmentID':'"+dep.getID()+"'}");
		HttpServletResponse response = ContextHolder.findInstance()
				.getResponse();
		try {
			PrintWriter out = response.getWriter();
			out.write("user=" + json);
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
